
 //wx.setNavigationBarTitle({  title: '000'});
 var windowWidth = require ('../../js/getWindowSize.js').getWindowSize().windowWidth;
 var tabbarJs = require("../tabbar/tabbar.js");
 var init = tabbarJs.init;
 var onTabbarItemTap = tabbarJs.onTabbarItemTap;
 var app = getApp();
 var requestMode = require('../../js/requestsMode.js');
 var session = require('../../js/session.js');
 var constants = require('../../js/constants.js');
 var Tools = require('../../js/Tools.js');
 //var Tabbar = tabbarJs.Tabbar;
 //var setTabbarData = tabbarJs.setTabbarData;
 // tabbar 对象的数据
 var modelImgSrc = '../../images/';
 const PageData={
     data: {
         hdimg: [],
         search_bg:'../../images/search.png',
         circular: true,
         indicatorcolor: "#000",
         vertical: false,
         imgheights: [],
         imgwidth: 750,
         current: 0,
         indicatorDots: true,
         autoplay: true,
         interval: 5000,
         duration: 1000,
         userInfo: {},
         news_number:10,
         title_boxWidth:windowWidth,
         first_img: {},
         two_img:{},
         three_img:{},
         four_img:{},
         ///当前页
         page:1,
         ///总页数
         page_total : 0,
         limitTime_buy_img:modelImgSrc+'limitTime_buy.png',
         promotion_title_img:modelImgSrc+'promotion_title.png',
         limitTimeBuyTitle : modelImgSrc+'limitTimeBuyTitle.png',
         promotion_content_img:[],
         promotion_content_img_length : 0,
         promotionImgHeight : [],
         jhDataForTabbar :Tools.tabBarItems(0,0),
         showSearchList:false,
         getSearch:[],
         getSearchLength : 0,
         searchPrompt:'搜索关键词如：牛肉、啤酒、餐盒等...',
         cursorImg : modelImgSrc+'cursor.png',
         closeSearchContent : modelImgSrc+'redClose.png',
         setCloseBg:'none',
         limitBuyLength : 0,
         LimitTimeBg : modelImgSrc+'LimitTimeBg.png',
         timeD : modelImgSrc+'timeD.png'
     },
     sss:function(){
         //console.log("ssssss",this.data.title_boxWidth);
     },
     //分类列表跳转分类
     seeType:function(e){
        let pid = e.currentTarget.dataset.pid;
         wx.navigateTo({
             url: '../type/type?goods_type_pid='+pid+'&fromIndexType=true'
         })
     },
     //轮播跳转查看详情或分类
     seeDetails:function(e){
         let detailId =e.currentTarget.dataset.viewpointUuid;
         let type = e.currentTarget.dataset.type;
         let goods_type_pid = e.currentTarget.dataset.goods_type_pid;
         let goods_id = e.currentTarget.dataset.goods_id;
         let goods_type_id = e.currentTarget.dataset.goods_type_id;
         if(type == 1){
             wx.navigateTo({
                 url: '../type/type?id='+detailId+"&goods_type_pid="+goods_type_pid+"&goods_type_id="+goods_type_id
             })
         }else{
             wx.navigateTo({
                 url: '../type/detailed/detailed?id='+goods_id
             })
         }
     },
     toDetailed: function (e) {
         let goods_id = e.currentTarget.dataset.id;
         wx.navigateTo({
             url: '../type/detailed/detailed?id='+goods_id
         })
     },
     //轮播图片加载完毕
     imageLoad: function (e) {
         var imgwidth = e.detail.width,
             imgheight = e.detail.height,
             ratio = imgwidth / imgheight;
         var viewHeight = imgwidth / ratio;
         var imgheights = this.data.imgheights;
         imgheights.push(viewHeight);
         this.setData({
             imgheights: imgheights
         })
     },
     //限时抢购图片加载完毕
     limitTime_buy_img_load:function(e){
         const limit_imgwidth = e.detail.width,
             limit_imgheight = e.detail.height,
             limit_promotion_ratio = limit_imgwidth / limit_imgheight,
             index = e.target.dataset.index;
         const limit_need_width = windowWidth*2;
         const limit_need_height = limit_need_width/limit_promotion_ratio;
         let limitBuy = this.data.limitBuy;
         limitBuy[index]['need_height'] = limit_need_height;
         limitBuy[index]['need_width'] = limit_need_width;
         this.setData({
             limitBuy:limitBuy
         })
     },
     SwiperChange: function (e) {
         this.setData({ current: e.detail.current})
     },
     /*查询聚焦*/
     inputFocus:function(){
         this.setData({
             showSearchList:true
         })
     },
     tapBody: function () {
         this.setData({
             showSearchList:false
         })
     },
     //查询框变化------------------请求接口获取数据
     searchChange: function (e) {
         let searchContent = e.detail.value;
         let That = this;
         if(searchContent){
             Tools.showLoaddingDefault();
             requestMode.sendRequest('goods/search','POST',{goods_name : searchContent},{}).then(function(success){
                 Tools.hideLoaddingToast();
                 if(success.data.code == 200){
                     That.setData({
                         getSearch : success.data.data,
                         getSearchLength : success.data.data.length,
                         setCloseBg:'#fff'
                     })
                 }else{
                     That.setData({
                         searchPrompt : '暂未搜索到相关商品信息...',
                         getSearch : [],
                         getSearchLength : 0,
                         setCloseBg:''
                     })
                 }
             },function(){
                 Tools.hideLoaddingToast();
                 That.setData({
                     searchPrompt : '暂未搜索到相关商品信息...',
                     getSearch : [],
                     getSearchLength : 0,
                     setCloseBg:''
                 })
             })
         }else{
             this.setData({
                 showSearchList:false,
                 setCloseBg:''
             })
         }
     },
     onShow: function () {
         this.setData({
             showSearchList:false,
             setCloseBg:'',
             searchPrompt : '搜索关键词如：牛肉、啤酒、餐盒等...',
             getSearch : [],
             getSearchLength : 0
         })
     },
     //关闭查询内容
     closeSearchContent: function () {
         this.setData({
             showSearchList:false,
             setCloseBg:''
         })
     },
     onTabbarItemTap:onTabbarItemTap,
     onReady: function () {

     },

     ///获取下面列表
     get_promotion_content:function(){
         let This = this;
         requestMode.sendRequest('goods/activity-list','POST',{type:1,page:(This.data.page),size:constants.size},{}).then(function(res){
             let code = res.data.code;
             Tools.hideLoaddingToast();
             This.setData(
                 {
                     page_total : res.data.data.page_total
                 }
                 );
             if(code == 200){
                 let getData = res.data.data.list;
                 let promotion_content_img = This.data.promotion_content_img;
                 getData.forEach(function(value){
                     promotion_content_img.push(value);
                 });
                 This.setData({
                     promotion_content_img:promotion_content_img,
                     promotion_content_img_length : promotion_content_img.length
                 })
             }
           /*  else{
                 Tools.showErrorToast(res.data.message);
             }*/
         },function(){
             Tools.hideLoaddingToast();
             Tools.showWrongNetWorkToast();
         })
     },
    ///点击列表进入详情
     enterDetailEvent : function (res) {
         let detailId =res.currentTarget.dataset.id;
         wx.navigateTo({
             url: '../type/detailed/detailed?id='+detailId
         });
     },
     ///获取列表
     getTypeList:function(){
         let This = this;
         requestMode.sendRequest('goods/types','POST',{pid:0,sort_type:1},{}).then(function(res){
             let code = res.data.code;
             if(code == 200){
                 let getTypeList = res.data.data.list.slice(0,4);
                 This.setData({
                     first_img: getTypeList[0],
                     firstType:getTypeList[0]['id'],
                     two_img:getTypeList[1],
                     twoType:getTypeList[1]['id'],
                     three_img:getTypeList[2],
                     threeType:getTypeList[2]['id'],
                     fourType:getTypeList[3]['id'],
                     four_img:getTypeList[3]
                 });
                 Tools.hideLoaddingToast();
             } else{
                 Tools.showErrorToast(res.data.message);
             }
         },function(){
                Tools.showWrongNetWorkToast();
         });
         wx.stopPullDownRefresh()
     },
     //公共倒计时方法
     leastTime: function () {
         let data = this.data.limitBuy;
         let that = this;
         setInterval(function () {
             data.forEach(function (v,i) {
                 let startTime = v['seckill_start_time'];
                 let endTime = v['seckill_end_time'];
                 let startNewDate = Number(new Date(startTime));
                 let endNewDate =Number(new Date(endTime)) ;
                 let nowNewDate = Number(new Date());
                 if( nowNewDate < startNewDate){
                     let DifferTime = startNewDate - nowNewDate;
                     let leastDay = parseInt( new Date(DifferTime)/1000/3600/24 );          //剩余天数
                     let overHour = parseInt( (DifferTime-leastDay*24*3600*1000)/1000/3600 );//剩余小时

                     let leastMinutes =  DifferTime - (overHour*3600*1000) - (leastDay*24*3600*1000);                         //剩余分钟的毫秒数
                     let overMinutes =parseInt( leastMinutes/1000/60  );                    //剩余分钟

                     let leastSeconds = leastMinutes - (overMinutes*60*1000);                       //剩余秒的毫秒数
                     let overSeconds = parseInt( leastSeconds/1000 );                        //剩余秒

                     v['leastX']=overHour;
                     v['leastF']=overMinutes;
                     v['leastM']=overSeconds;
                     v['leastT'] = leastDay;
                     v['startOrEnd']='离活动开始有'
                 }else if(nowNewDate <= endNewDate && nowNewDate >= startNewDate){
                     let DifferTime = endNewDate - nowNewDate;
                     let leastDay = parseInt( new Date(DifferTime)/1000/3600/24 );
                     let overHour = parseInt( (DifferTime-leastDay*24*3600*1000)/1000/3600 );
                     let leastMinutes =  DifferTime - (overHour*3600*1000) - (leastDay*24*3600*1000);
                     let overMinutes =parseInt( leastMinutes/1000/60  );
                     let leastSeconds = leastMinutes - (overMinutes*60*1000);
                     let overSeconds = parseInt( leastSeconds/1000 );
                     v['leastX']=overHour;
                     v['leastF']=overMinutes;
                     v['leastM']=overSeconds;
                     v['leastT'] = leastDay;
                     v['startOrEnd']='活动时间剩余'
                 }else{
                     data.splice(i,1)
                 }
             });
             that.setData({
                 limitBuy :data
             })
         },1000)
     },
     getActiveList:function(){
         let This = this;
         requestMode.sendRequest('goods/activity-list','POST',{type:2,page:1,size:constants.size},{}).then(function(res){
             let code = res.data.code;
             Tools.hideLoaddingToast();
             if(code == 200){
                 let getData = res.data.data.list;
                 getData.forEach(function (v) {
                     v['seckill_start_time'] = v['seckill_start_time'].replace(/-/g,'/');
                     v['seckill_end_time'] = v['seckill_end_time'].replace(/-/g,'/');
                 });
                 This.setData({
                     limitBuy:getData,
                     limitBuyLength : getData.length
                 });
                 This.leastTime();
             }
          /*   else{
                 Tools.showErrorToast(res.data.message);
             }*/
         },function(){
             Tools.hideLoaddingToast();
             Tools.showWrongNetWorkToast();
         });
         wx.stopPullDownRefresh()
     },
     onLoad:function(){
         let This =  this;
         Tools.requestBuyCarCount(false,this,0);
         Tools.showLoaddingDefault();
         requestMode.sendRequest('goods/banners','POST',{},{}).then(function(res){
             Tools.hideLoaddingToast();
             if( res.data.code == 200){
                let getBanner = res.data.data.list;
                 This.setData({
                     hdimg:getBanner
                 });
             } else{
                 Tools.showErrorToast(res.data.message);
             }
         },function(){
             Tools.hideLoaddingToast();
             Tools.showWrongNetWorkToast();
         });
         this.getTypeList();this.getActiveList();this.get_promotion_content();
     },
     onPullDownRefresh: function () {
         this.setData({page:1,promotion_content_img:[]});
         Tools.showLoaddingDefault();
         this.onLoad();
     },
     onReachBottom: function () {
         if (this.data.page >= this.data.page_total)
         {
             return;
         }
         let This = this;
         this.setData({
             page:This.data.page+1
         });

         Tools.showLoaddingDefault();
         this.get_promotion_content();
     }
 };
 init(PageData);

